package by.exposit.controller;

import by.exposit.domain.*;
import by.exposit.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by HaRiA on 30.07.2014.
 */
@Controller
@RequestMapping("projects")
public class ProjectController {

    @Autowired
    FieldService fieldService;

    @Autowired
    ProjectService projectService;

    @Autowired
    UserService userService;

    @Autowired
    RequestService requestService;

    @Autowired
    PocketService pocketService;

    private Date date = new Date();

    @RequestMapping(value = "new",method = RequestMethod.GET)
    public ModelAndView createProject(HttpSession session)
    {
               ModelAndView mav = new ModelAndView();
        List<Field> fields = fieldService.fieldList();
        mav.addObject("fields", fields);
        mav.setViewName("../projects/new");
        return mav;

    }
    //Create project with redirect after creation on previous page
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public  ModelAndView createProject(@RequestParam(value = "name",required = true) String name,
                                       @RequestParam(value = "fieldId",required = true) Long fieldId,
                                       HttpServletRequest request, HttpSession session)
    {
        Pocket pocket = new Pocket();

        pocketService.savePocket(pocket);



        Project project =  new Project(name,(User)session.getAttribute("user"), fieldService.retrieveField(fieldId),pocket);

        projectService.saveProject(project);
        String prev_page = (String) session.getAttribute("prevpage");
        ModelAndView mav = new ModelAndView();
        mav.setViewName(prev_page);

        return  mav;
    }

    //returns list of users project
    @RequestMapping(value="usersProjects/{userId}",method=RequestMethod.GET)
    public ModelAndView getListOfUsersProjects(
            @PathVariable String userId,

            HttpSession session
    )
    {
        ModelAndView mav = new ModelAndView();
        String prev_page =(String) session.getAttribute("prevpage");

        // (User) session.getAttribute("user");

        List<Project> projects = projectService.projectUserList(Long.valueOf(userId));
        if(!projects.isEmpty()) {
            mav.addObject("projects", projects);
        }
        mav.addObject("prev_page",prev_page);
        mav.setViewName("projects/list");
        return mav;
    }

    //takes information about project, with spetial functions only for owner
    @RequestMapping(value = "project/{projectId}",method=RequestMethod.GET)
    public ModelAndView projectInfo(@PathVariable String projectId,HttpSession session)
    {
      ModelAndView mav = new ModelAndView();

        Project project = projectService.retrieveProject(Long.valueOf(projectId));
        mav.addObject("project",project);
        mav.addObject("current_user",(User)session.getAttribute("user"));
        mav.setViewName("projects/project");
        session.setAttribute("prevpage", "redirect:/projects/list/" + project.getUser().getUserId());
        return mav;

    }

    //delete
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public ModelAndView delete( @RequestParam(value = "projectId",required = true) String projectId,
            HttpSession session)
    {

        ModelAndView mav = new ModelAndView();
        Project project = projectService.retrieveProject(Long.valueOf(projectId));
        Request request = requestService.getRequestByProject(project);
        String home_page =(String)session.getAttribute("homepage");
        User user =(User)session.getAttribute("user");
        if(user.getUserId() == project.getUser().getUserId()) {
          if(request!=null)
          {
              requestService.remove(request.getRequestId());
          }
        projectService.remove(Long.valueOf(projectId));

        }
        mav.setViewName("../"+home_page);
        return mav;
    }
}



